package com.cci.kangdao.dao;

import com.cci.kangdao.dao.model.*;
import com.cci.kangdao.model.AuthInfoModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

@MapperScan
public interface LoginDao {
		
	/**
	 * 获取用户
	 * @param userId
	 * @return
	 */
	public UserT getUserByid(String userId);

	/**
	 * 获取用户
	 * @param adUserId
	 * @return
	 */
	public UserT getUserByAdUserId(String adUserId);

	/**
	 * 获取用户
	 * @param adUserId
	 * @return
	 */
	public UserT getUserAndGroupNoByAdUserId(Long adUserId);
	
	
	/**
	 * @param adUserId
	 * @return
	 */
	public AuthInfoModel getAuthInfoByAdUserId(String adUserId);

	/**
	 * 根据userid  获取  渠道编号
	 * @param userId
	 * @return
	 */
	 AuthInfoModel getAuthInfoByUserId(Long userId);

	
	
	/**
	 * 根据用户名获取用户
	 * @param userName
	 * @return
	 */
	public UserT getUserByUserName(String userName);
	
	/**
	 * 根据手机号获取用户
	 * @param phone
	 * @return
	 */
	public List<UserT> getUserByPhone(String phone);
	
	/**
	 * 根据id号获取用户的企业部门信息
	 * @param id
	 * @return
	 */
	public UserT getUserCompanyDept(long id);
	
	/**
	 * 根据id获取服务站信息
	 * @param phone
	 * @return
	 */
	public ServiceStationT getServiceStationById(long id);
		
	/**
	 * 获取用户的所有功能
	 * @param userid
	 * @return
	 */
	public List<FunctionT> getAllFunctionByUserid(long userid);

	/**
	 * 根据list 获取功能
	 * @param functionList
	 * @return
	 */
	 List<FunctionT> getAllFunctionByList(@Param("functionList") List<Long> functionList);

	/**
	 * 插入前台系统日志
	 * @param bean
	 * @return
	 */
	public boolean insertRecordLogs(RecordLogsT bean);
	
	/**
	 * 修改前台系统日志
	 * @param bean
	 * @return
	 */
	public int updateRecordLogs(RecordLogsT bean);

	/**
	 * 添加用户
	 *
	 * @param bean
	 * @return
	 */
	public int insertUser(UserT bean);

	/**
	 * 添加用户 添加上省市县信息
	 *
	 * @param bean
	 * @return
	 */
	public int insertUserAddSeat(UserT bean);

	/**
	 * 添加用户角色
	 *
	 * @param bean
	 * @return
	 */
	public int insertUserRole(Map map);

	/**
	 * 修改用户token
	 * @param bean
	 * @return
	 */
	public int updateUserToken(UserT bean);
	
	/**
	 * 修改用户信息
	 * @param bean
	 * @return
	 */
	public int updateUser(UserT bean);
	
	/**
	 * 用户首次修改密码后更改1，表示下次不用修改
	 * @param bean
	 * @return
	 */
	public int updateUserIsPwd(UserT bean);
	
	/**
	 * 用户组关联角色
	 * @param userGroupId
	 * @return
	 */
	public List<Long> userGroupHasRoleId(Long userGroupId);


    /**
     * 获取app用户组下普通用户角色   根据userGroupHasRoleId方法sql修改
     *
     * @param userGroupId
     * @return
     */
    @Select("select a.ID from UserGroupHasRole_T a\n" +
            "inner join Roles_T b on a.RoleId = b.ID\n" +
            "where a.Flag = 0 and a.UserGroupId = #{userGroupId} and b.RolesNo = 'R0002' and b.Flag = 0")
    public List<Long> userGroupHasYHRole(Long userGroupId);
	
	/**
	 * 获取用户组关联角色列表
	 * @param userGroupId
	 * @return
	 */
	List<Map<String, Object>> userGroupHasRoles(Long userGroupId);
	
	/**
	 * 通过用户ID查询用户信息
	 * @param userId
	 * @return
	 */
	UserT getUserByUserId(Long userId);
	
	/**
	 * 获取用户角色列表
	 * @param userId
	 * @return
	 */
	List<Map<String, Object>> getUserRoles(Long userId);
	
	/**
	 * 获取角色数据
	 * @param roleId
	 * @return
	 */
	Map<String, Object> getRoleInfo(Long roleId);
	
	/**
	 * 角色用户关系解绑
	 * @param userId
	 */
	void delUserRoleRel(String userId);
	
	/**
	 * 插入用户与角色关联关系
	 * @param map
	 * @return
	 */
	int insertUserRoleByScan(Map<String, Object> map);
	
	/**
	 * 修改用户信息
	 * @param user
	 */
	void authChange(UserT user);
	
	/**
	 * 插入扫码授权记录
	 * @param map
	 */
	void insertScanRecord(Map<String, Object> map);
	
	/**
	 * 插入操作日志
	 * @param bean
	 * @return
	 */
	int insertSystemLogs(SystemLogsT bean);
	
	/**
	 * 添加关联记录
	 * @param userName
	 * @param cId
	 * @return
	 */
	int addRelationShip(@Param("userName")String userName, @Param("cId")String cId);
	
	/**
	 * 修改关联记录
	 * @param userName
	 * @param cId
	 * @return
	 */
	int updRelationShip(@Param("userName")String userName, @Param("cId")String cId);
	
	/**
	 * 删除关联记录
	 * @param userName
	 * @return
	 */
	int delRelationShip(@Param("userName")String userName);
	
	String getClientId(String userName);
	
	Long validCheck(@Param("userId")String userId, @Param("roleId")Long roleId);
	
	String getApiDomain();

	/**
	 * 查询该企业是否有GM权限
	 * @param companyId
	 * @return
	 */
	@Select("select COUNT(1) from GMAuthCompany_T where CompanyId = #{companyId} and CompanyType = 1 and Flag = 0")
    int getGMAuthStatusByCompanyId(@Param("companyId")long companyId);

	/**
	 * 判断是否为服务商的员工
	 * @param serviceStationID
	 * @return
	 */
	@Select("select COUNT(1) from GMAuthCompany_T where CompanyId = #{serviceStationID} and CompanyType = 2 and Flag = 0")
	int getGMAuthStatusByServiceStationId(long serviceStationID);
	
	@Select("select * from User_T where UPPER(Email) = UPPER(#{email}) and Flag = 0")
	UserT getUserByEmail(String email);
	
	@Select("select count(1) from User_T where UPPER(Email) = UPPER(#{email}) and Flag = 0")
	int getUserCountByEmail(String email);
	
	@Update("update User_T set Email = #{email} where ID = #{userId}")
	int setEmail(@Param("userId") long userId, @Param("email") String email);
	
	@Update("update User_T set Email = '' where ID = #{userId}")
	int blankEmail(@Param("userId") long userId);


    @Select("select * from User_T where UserName = #{userName} and Flag = 0")
    UserT getUserMsgByUserName(@Param("userName") String userName);


    @Update("update User_T set UserSource = #{userSource} where ID = #{userId}")
    int updateUserSourceById(@Param("userSource") int userSource, @Param("userId") long userId);
    
    
    int updateUserCompanyAndUserTypeById(@Param("userId")Long userId, @Param("userCompanyName")String userCompanyName, @Param("guidanzUserType")Integer guidanzUserType);
	@Select("select * from SSRepair_T where ServiceStationID = #{serviceStationID} and Flag = 0")
	List<ServiceStationT> getBusinessNo(@Param("serviceStationID")long serviceStationID);



	//根据用户名获取用户信息       参考getUserByid的sql    这里传手机号为参数和用户名匹配
	@Select("select top 1 a.*,b.IsType,b.GroupName as usergroupName,c.GroupName as usergroupSubName, ct.CompanyName as companyName, ss.StationName as stationName\n" +
			"from User_T a left join Usergroup_T b on a.UsergroupID=b.ID left join UsergroupSub_T c on a.UsergroupSubID=c.ID \n" +
			"left join Company_T ct on a.CompanyID = ct.ID \n" +
			"left join ServiceStation_T ss on a.ServiceStationID = ss.ID\n" +
			"where a.Flag=0 and (a.UserName = #{phone} or a.Phone = #{phone})\n" +
			"order by a.ID desc")
	UserT getUserTByUserName(@Param("phone") String phone);


	@Select("select * from User_T where ID = #{userId} and Flag = 0")
	UserT getUserMsgById(@Param("userId") long userId);

	/**
	 * 添加用户  新
	 *
	 * @param bean
	 * @return
	 */
	public int insertUserNew(UserT bean);

	void updateUserTokenAddSeat(UserT userT);

	//获取用户的所有动作
	public List<ActionT> getAllActionByUserid(@Param("userId") Long userId);
}